package com.winner.scan.winner_scan_pluge

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import androidx.annotation.NonNull

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result

/** WinnerScanPlugePlugin */
class WinnerScanPlugePlugin: FlutterPlugin, MethodCallHandler, EventChannel.StreamHandler  {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private lateinit var channel : EventChannel
  private lateinit var methodChannel : MethodChannel
  private var eventSink: EventChannel.EventSink? = null

  private var scanActton:String? ="com.android.server.scannerservice.broadcast"

  private lateinit var context: Context



  override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
    if(Build.MODEL=="AUTOID Q9N"){
      scanActton="com.android.server.aa"
    }
    channel = EventChannel(flutterPluginBinding.binaryMessenger, "winner_scan_pluge_get")
    methodChannel = MethodChannel(flutterPluginBinding.binaryMessenger, "winner_scan_pluge_register")
    context = flutterPluginBinding.applicationContext;
    BoardCastSetting();
    methodChannel.setMethodCallHandler(this)
    channel.setStreamHandler(this)


  }

  override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
    if (call.method == "register") {
      val intentFilter = IntentFilter(scanActton)
      intentFilter.priority = Int.MAX_VALUE
      context.registerReceiver(scanReceiver, intentFilter)
      result.success(true)
    } else {
      result.notImplemented()
    }
  }
  var scanReceiver: BroadcastReceiver = object : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
      if (intent.action == scanActton) {
        val code = intent.getStringExtra("scannerdata")
        eventSink?.success(code)
      }
    }
  }
  override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
    context.unregisterReceiver(scanReceiver);
  }
  private fun BoardCastSetting() {
    val intent = Intent("com.android.scanner.service_settings")
    //修改广播名称：修改扫描工具广播名，接收广播时也是这个广播名
    intent.putExtra("action_barcode_broadcast", scanActton)
    //条码发送方式：广播；其他设置看文档
    intent.putExtra("barcode_send_mode", "BROADCAST")
    //键值，一般不改
    intent.putExtra("key_barcode_broadcast", "scannerdata")
    //声音
    intent.putExtra("sound_play", true)
    //震动
    intent.putExtra("viberate", true)
    //其他参数设置参照：Android扫描服务设置.doc
    context.sendBroadcast(intent)
  }

  override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
    eventSink = events
  }

  override fun onCancel(arguments: Any?) {
    eventSink=null;
  }
}
